

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SettingsForm extends IEWForms {
	
	/**
	 *  Form for doing program settings such as:
	 *  	Profile Setup 
	 */
	private static final long serialVersionUID = 1L;
	
	JPanel jpnAvailableActions = new JPanel(); // any common buttons
	JPanel jpnAvailableSettings = new JPanel(null); // all other thing
	
	JButton jbtSaveButton = new JButton();
	JButton jbtCloseButton = new JButton();
	
	JButton jbtAddNewLanguage = new JButton();
	JButton jbtAddNewLanguage2 = new JButton();
	
	// settings fields
	JComboBox  jcbProfile = new JComboBox();
	JTextField jtfTimeaInterval = new JTextField();
	JTextField jtfAskCount = new JTextField();
	JComboBox  jcbNativeLanguage = new JComboBox();
	JComboBox  jcbStudyLanguage = new JComboBox();
	// settings field labels
	JLabel jlbProfile = new JLabel();
	JLabel jlbTimeInterval = new JLabel();
	JLabel jlbAskCount = new JLabel();
	JLabel jlbNativeLanguage = new JLabel();
	JLabel jlbStudyLanguage = new JLabel();
	
	public SettingsForm (JFrame parent) {
		super(parent, "change settings");
		doInitForm();
		this.setVisible(true);
	}

	@Override
	public void doInitForm() {
		jcbProfile.setEditable(true);
		this.setLayout(new BorderLayout());
		
		setLabelsName();
		int fWidth = myParent.getWidth();
		int large = (int)(fWidth * 0.7);
		int smal = (int)(fWidth * 0.27);
		int magic = 210; // pixel for good button
		
		if (fWidth <= magic){
			jbtCloseButton.setPreferredSize(new Dimension((int)(fWidth * 0.47), 25));
			jbtSaveButton.setPreferredSize(new Dimension((int)(fWidth * 0.47), 25));
			jpnAvailableActions.setLayout(new FlowLayout(FlowLayout.CENTER, (int)(fWidth * 0.02), 0));
		}
		// add actions components
		jpnAvailableActions.add(jbtCloseButton);
		jpnAvailableActions.add(jbtSaveButton);
		// settings setup
		jlbProfile.setBounds(5, 5, large, 25);
		jcbProfile.setBounds(large + 5, 5, smal, 25);
		jlbNativeLanguage.setBounds(5, 35, large - 35, 25);
		jcbNativeLanguage.setBounds(large - 30, 35, smal, 25);
		jbtAddNewLanguage.setBounds(large - 25 + smal, 35, 25, 25);
		jlbStudyLanguage.setBounds(5, 65, large - 35, 25);
		jcbStudyLanguage.setBounds(large - 30, 65, smal, 25);
		jbtAddNewLanguage2.setBounds(large - 25 + smal, 65, 25, 25);
		jlbTimeInterval.setBounds(5, 95, large, 25);
		jtfTimeaInterval.setBounds(large + 5, 95, smal, 25);
		jlbAskCount.setBounds(5, 125, large, 25);
		jtfAskCount.setBounds(large + 5, 125, smal, 25);
			//add components
		jpnAvailableSettings.add(jlbProfile);
		jpnAvailableSettings.add(jcbProfile);
		jpnAvailableSettings.add(jlbNativeLanguage);
		jpnAvailableSettings.add(jcbNativeLanguage);
		jpnAvailableSettings.add(jbtAddNewLanguage);
		jpnAvailableSettings.add(jlbStudyLanguage);
		jpnAvailableSettings.add(jcbStudyLanguage);
		jpnAvailableSettings.add(jbtAddNewLanguage2);
		jpnAvailableSettings.add(jlbTimeInterval);
		jpnAvailableSettings.add(jtfTimeaInterval);
		jpnAvailableSettings.add(jlbAskCount);
		jpnAvailableSettings.add(jtfAskCount);
		
		
		this.add(jpnAvailableActions, BorderLayout.SOUTH);
		this.add(jpnAvailableSettings, BorderLayout.CENTER);
		setButtonListeners();		
	}

	@Override
	public void setButtonListeners() {
		ActionListener addLanguage = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				showNewLanguageForm();
			}
		};
		
		jbtAddNewLanguage.addActionListener(addLanguage);
		jbtAddNewLanguage2.addActionListener(addLanguage);
		
		jbtCloseButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				doCloseForm();
			}
		});
		
		jcbProfile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				EW_Profiles profile = new EW_Profiles(jcbProfile.getSelectedItem().toString());
				profile = profile.getProfileByName(profile.getName());
				jcbNativeLanguage.setSelectedItem(profile.getNativeLanguage());
				jcbStudyLanguage.setSelectedItem(profile.getStudyLanguage());
				jtfAskCount.setText(((Integer)profile.getAskCount()).toString());
				jtfTimeaInterval.setText(((Integer)profile.getRestTime()).toString());
				
			}
		});
		
		jbtSaveButton.addActionListener(new ActionListener() {
		
			@Override
			public void actionPerformed(ActionEvent arg0) {
				EW_Profiles profile = new EW_Profiles(jcbProfile.getSelectedItem().toString());
				profile.setNativeLanguage(EW_Languages.GetOneRowByName(jcbNativeLanguage.getSelectedItem().toString()));
				profile.setStadyLanguage(EW_Languages.GetOneRowByName(jcbStudyLanguage.getSelectedItem().toString()));
				profile.setRestTime(Integer.parseInt(jtfTimeaInterval.getText()));
				profile.setAskCount(Integer.parseInt(jtfAskCount.getText()));
				profile.setLoadByDefoult(true);
				profile.insertCurRecord();
				profile.setNewDefaultCurrentProfile();
				myParent.profile = profile;
			}
		});
		
	}
	
	private void showNewLanguageForm(){
		new LanguagesForm(myParent, this);	
	}
	
	private void doCloseForm(){
		myParent.changeTimerStatus();
		myParent.updateVisualCounts();
		jbtAddNewLanguage.removeActionListener(jbtAddNewLanguage.getActionListeners()[0]);
		jbtAddNewLanguage2.removeActionListener(jbtAddNewLanguage2.getActionListeners()[0]);
		jbtCloseButton.removeActionListener(jbtCloseButton.getActionListeners()[0]);
		jbtSaveButton.removeActionListener(jbtSaveButton.getActionListeners()[0]);
		this.dispose();
	}

	@Override
	public void setLabelsName() {
		//action button setup
		jbtSaveButton.setText("Сохранить");
		jbtCloseButton.setText("Закрыть");
		//forms components setup
		jlbProfile.setText("Выбирете профиль");
		jlbTimeInterval.setText("секун между занятиями");
		jlbAskCount.setText("Количество вопросов в упражнении");
		jlbNativeLanguage.setText("Выберите родной язык");
		jlbStudyLanguage.setText("Выберите изучаемый язык");
		setLanguageItems();
		
		jbtAddNewLanguage.setIcon(new ImageIcon(this.getClass().getResource("resurse/add.gif")));
		jbtAddNewLanguage2.setIcon(new ImageIcon(this.getClass().getResource("resurse/add.gif")));
	}
	
	public void setLanguageItems(){
		jcbNativeLanguage.removeAllItems();
		EW_Languages myI = new EW_Languages();
		ArrayList<EW_Languages> data = myI.getListInDB();
		for (int i = 0; i < data.size(); i++) {
			jcbNativeLanguage.addItem(data.get(i).getName());
			jcbStudyLanguage.addItem(data.get(i).getName());
		}
		
		EW_Profiles profile = new EW_Profiles("");
		ArrayList<EW_Profiles> profiles = profile.getAllProfilesInDB();
		for (int i = 0; i < profiles.size(); i++) {
			jcbProfile.addItem(profiles.get(i).getName());
		}
		try {
			jcbProfile.setSelectedItem(myParent.profile.getName());
			jcbNativeLanguage.setSelectedItem(myParent.profile.getNativeLanguage());
			jcbStudyLanguage.setSelectedItem(myParent.profile.getStudyLanguage());
			jtfAskCount.setText(((Integer)myParent.profile.getAskCount()).toString());
			jtfTimeaInterval.setText(((Integer)myParent.profile.getRestTime()).toString());
		} catch (Exception e) {e.printStackTrace(); };
		
	}

}
